<!DOCTYPE html>
<html>

<head>

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>首页</title>
    <link href="../css/plugins/summernote/summernote.css" rel="stylesheet">

    <style>

        .jqGrid_wrapper {
            padding: 10px 15px;
            border: none;
        }

        .jqGrid_wrapper td {
            white-space: normal !important;
            height: auto;
        }

        .head-nav {
            display: inline-block;
            vertical-align: middle;
        }

        .search-button-div {
            position: absolute;
            top: 6px;
            right: 16px;
            font-size: 14px;
            cursor: pointer;
        }

        .add-button-div {
            /*position: absolute;*/
            /*top: 15px;*/
            /*right: 25px;*/
            margin-right: 28px;
            float: right;
            font-size: 14px;
            cursor: pointer;
        }

        .transfer-button {
            color: white !important;
            font-size: 11px;
            padding: 3px 8px;
        }

        .remark {
            height: 60px;
            width: 100%;
            padding: 10px;
            /*background-color: #eeeeee;*/
            resize: none;
            border-radius: 5px;
            border: 0;
            display: block;
        }

        .margin-left-1 {
            /*margin-left: 10px !important;*/
        }

        .margin-left-2 {
            /*margin-left: 20px !important;*/
        }

        .margin-right-1 {
            margin-right: 10px;
        }

        .margin-right-2 {
            margin-right: 20px;
        }

        .element-right {
            text-align: right;
        }

        .element-center {
            text-align: center;
        }

        .modal-dialog {
            width: 60% !important;
        }

        .fa-plus-circle:before {
            content: "\f055";
            font-size: 26px;
            background-color: #FFFFFF;
            border-radius: 50%;
            color: #f8ac59;
            vertical-align: middle;
        }

        .float-left {
            float: left;
        }

        .margin-left-5 {
            margin-left: 5%;
        }

        /*#modal-transfer-div {*/
        /*z-index: 9999 !important;*/
        /*}*/

        .warning-color {
            color: red !important;
        }

        #div_tab .nav-tabs > li > a {
        	font-size:14px;
            color: #ccc;
            border: 1px solid #ddd;
            border-bottom: none;
        }

        #div_tab .nav-tabs > li.active > a {
            color: #42D0A7;
            border-bottom: 1px solid #fff;
        }

        option:disabled {
            color: #dcdcdc;
        }

        .ui-jqgrid-hdiv, .ui-jqgrid-hbox, .ui-jqgrid-htable, .ui-jqgrid-bdiv, .ui-jqgrid-btable {
            width: 100% !important;
            padding: 0 !important;
            margin: 0 !important;
        }
        
        .icon-btn {
        	display: inline-block;
    		width: 40px;
    		height: 40px;
    		line-height: 40px;
    		text-align: center;
    		border-radius: 50%;
    		color: #fff;
    		font-size: 24px;
        }
        .icon-btn.icon-tongji {
        	background-color: #53CFAF;
        }
        .icon-btn.icon-zhuanzhen {
        	background-color: #39CEEB;
        	font-size: 20px;
        }
        .icon-btn.icon-jiahao {
        	background-color: #F4AA36;
        }
        .icon-btn+span {
        	font-size: 16px;
        }
    </style>
</head>

<body>
<!-- <div class="row">
	<div id="my-referral-count-button" class="add-button-div hide"
         onclick="loadTitle('/scheduling/referral_stat.html')">
        <i class="icon-btn iconfont icon-tongji"></i>
        <span style="vertical-align: text-bottom;">转诊统计</span>
    </div>
	<div id="my-referral-button" class="add-button-div"
         onclick="loadTitle('/referral/add_referral_title.html')">
        <i class="icon-btn iconfont icon-zhuanzhen"></i>
        <span style="vertical-align: text-bottom;">快速转诊</span>
    </div>
    <div id="my-patient-button" class="add-button-div"
         onclick="loadTitle('/emr/add_title.html')">
        <i class="icon-btn iconfont icon-jiahao"></i>
        <span style="vertical-align: text-bottom;">新建病历</span>
    </div>
</div> -->
<div class="row" id="div_tab">
    <div class="col-sm-12">
        <div class="panel blank-panel">
            <div class="panel-heading">
                <div class="panel-options head-nav" style="width: 100%;">
                    <ul class="nav nav-tabs">
                       <!--  <li id="my-patient-li" class="active">
                            <a data-toggle="tab" onclick="show(1)" style="font-size: 14px;font-weight: bold">我的患者</a>
                        </li> -->
                        <li id="out-patient-li" class="">
                            <a id="out-patient-a" data-toggle="tab" onclick="show(2)"
                               style="font-size: 14px;font-weight: bold">转出患者</a>
                        </li>
                      <!--   <li id="in-patient-li" class="">
                            <a data-toggle="tab" onclick="show(3)" style="font-size: 14px;font-weight: bold">转入患者</a>
                        </li> -->
                    </ul>
                </div>
                <div class="head-nav search-button-div" style="width: 25%">
                    <div class="input-group" style="padding: 10px 15px;">
                        <input type="text" class="form-control" id="search-key" placeholder="患者编号 | 姓名">
                        <span class="input-group-btn"><button type="button" class="btn btn-primary" onclick="search();">
                            搜索
                        </button></span>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<div class="row">
    <div class="col-sm-12">
        <div class="jqGrid_wrapper" id="my-patient-list" style="text-align: center;width: 100%;">
            <div id="my-patient-div" style="text-align: center;width: 100%;">
                <table id="my-patient" style="text-align: center;width: 100%;"></table>
            </div>

            <div id="no-records-1">没有查询记录！</div>
            <div class="pagination" id="pagination-1">
                <a class="first" id="first-btn-1" data-action="first"><i class="icon-double-angle-left"></i> </a>
                <a class="previous" id="previous-btn-1" data-action="previous"><i class="icon-angle-left"></i> </a>
                <input id="page-num-1" type="text" onkeydown="designatedPage(event, 1)"/>
                <a class="next" id="next-btn-1" data-action="next"><i class="icon-angle-right"></i> </a>
                <a class="last" id="last_btn_1" data-action="last"><i class="icon-double-angle-right"></i> </a>
            </div>
        </div>

        <div class="jqGrid_wrapper" id="out-patient-list" style="text-align: center">
            <div id="out-patient-div">
                <table id="out-patient"></table>
            </div>
            <div id="no-records-2">没有查询记录！</div>
            <div class="pagination" id="pagination-2">
                <a class="first" id="first-btn-2" data-action="first"><i class="icon-double-angle-left"></i></a>
                <a class="previous" id="previous-btn-2" data-action="previous"><i class="icon-angle-left"></i></a>
                <input id="page-num-2" type="text" onkeydown="designatedPage(event, 2)"/>
                <a class="next" id="next-btn-2" data-action="next"><i class="icon-angle-right"></i></a>
                <a class="last" id="last_btn_2" data-action="last"><i class="icon-double-angle-right"></i></a>
            </div>
        </div>

        <div class="jqGrid_wrapper" id="in-patient-list" style="text-align: center">
            <div id="in-patient-div">
                <table id="in-patient"></table>
            </div>
            <div id="no-records-3">没有查询记录！</div>
            <div class="pagination" id="pagination-3">
                <a class="first" id="first-btn-3" data-action="first"><i class="icon-double-angle-left"></i></a>
                <a class="previous" id="previous-btn-3" data-action="previous"><i class="icon-angle-left"></i></a>
                <input id="page-num-3" type="text" onkeydown="designatedPage(event, 2)"/>
                <a class="next" id="next-btn-3" data-action="next"><i class="icon-angle-right"></i></a>
                <a class="last" id="last_btn_3" data-action="last"><i class="icon-double-angle-right"></i></a>
            </div>
        </div>
    </div>
</div>

<!--<div class="row">-->
<!--<div class="col-sm-12">-->
<!--</div>-->
<!--</div>-->
<div id="modal-transfer-div" class="row modal fade" aria-hidden="true">
    <!--<div class="col-sm-12">-->
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-body">
                <div class="row margin-left-1" style="margin-top: 5px;">
                    <h3 style="text-align: center;clear:both;">转诊</h3>

                    <div class="col-sm-9">
                        <label style="font-size: 14px">基本信息</label>
                    </div>
                    <!--<div class="col-sm-3 element-right">-->
                    <!--<button id="lookup-patient-emr-btn" class="btn btn-w-m btn-primary inline-block">-->
                    <!--查看患者病历-->
                    <!--</button>-->
                    <!--</div>-->
                    <div class="col-sm-3 element-right hide">
                        <a data-dismiss="modal" id="lookup-patient-emr-btn" id="lookup">查看病历</a>
                    </div>
                </div>

                <div class="row margin-left-1" style="margin-top: 10px;">
                    <div class="margin-left-5 float-left">
                        <span>患者编号：</span>
                        <span id="my-patient-num"></span>
                    </div>
                    <div class="margin-left-5 float-left">
                        <span>姓名：</span>
                        <span id="my-patient-name"></span>
                    </div>
                    <div class="margin-left-5 float-left">
                        <span>性别：</span>
                        <span id="my-patient-sex"></span>
                    </div>
                    <div class="margin-left-5 float-left">
                        <span>年龄：</span>
                        <span id="my-patient-age"></span>
                    </div>
                    <div class="margin-left-5 float-left">
                        <span>医保类型：</span>
                        <span id="my-patient-type"></span>
                    </div>
                </div>
                <!--<div class="row margin-left-1" style="margin-top: 20px;">-->
                <!--<div class="col-sm-12">-->
                <!--<label style="font-size: 14px">转诊说明</label>-->
                <!--</div>-->
                <!--</div>-->
                <form id="referral-note-form">

                    <div class="row margin-left-1" style="margin-top: 20px;">
                        <div class="col-sm-2">
                            <label style="font-size: 14px">转诊患者是</label>
                        </div>
                        <div class="col-sm-4">
                            <select class="form-control" name="patient-type">
                                <option>住院患者</option>
                                <option>门诊患者</option>
                            </select>
                        </div>
                    </div>
                    <div class="row margin-left-1" style="margin-top: 20px;">
                        <div class="col-sm-2">
                            <label style="font-size: 14px">转诊备注</label>
                        </div>
                        <div class="col-sm-10">
                            <div class="radio i-checks">
                                <label class="no-padding">
                                    <input type="radio" name="new-remark" value="1"
                                           style="position: absolute; opacity: 0;">
                                    <i></i>患者病情稳定，转入下一级医院治疗。</label>
                            </div>
                            <div class="radio i-checks">
                                <label class="no-padding">
                                    <input type="radio" name="new-remark" value="2"
                                           style="position: absolute; opacity: 0;">
                                    <i></i>患者病情出现变化，转入上一级医院治疗。</label>

                            </div>
                            <div class="radio i-checks">
                                <div class="col-sm-2 no-padding">
                                    <label class="no-padding">
                                        <input type="radio" name="new-remark" value="3"
                                               style="position: absolute; opacity: 0;">
                                        <i></i>其他情况</label>
                                </div>
                                <div class="col-sm-10 no-padding">
                                    <input type="text" name="remark-text" class="form-control" disabled="disabled">
                                </div>
                            </div>
                            <!--<div class="radio">-->
                            <!--<label>-->
                            <!--<input type="radio" checked="checked" value="1" id="hospital-radio"-->
                            <!--name="hospital-doctor-radios">所有系统医生-->
                            <!--</label>-->
                            <!--</div>-->
                            <!--<div class="radio">-->
                            <!--<label>-->
                            <!--<input type="radio" value="2" id="doctor-radio" name="hospital-doctor-radios">-->
                            <!--指定医生-->
                            <!--</label>-->
                            <!--</div>-->
                        </div>
                    </div>
                    <!--<div class="row margin-left-1" style="margin-top: 10px;">-->
                    <!--<div class="margin-left-5" style="margin-right: 5%;">-->
                    <!--<textarea id="remark" class="remark form-control"-->
                    <!--placeholder="请输入转诊原因"-->
                    <!--oninput="setJudgementCount(this,'judgement_count');"></textarea>-->

                    <!--<div style="text-align: right;margin-top: 1vw;">-->
                    <!--<span id="judgement_count" style="color: #ddd;font-size: 12px">还可以输入128个字</span>-->
                    <!--</div>-->
                    <!--</div>-->
                    <!--</div>-->

                    <div class="row margin-left-1" style="margin-top: 20px;">
                        <div class="col-sm-12">
                            <label style="font-size: 14px">转诊通知</label>
                        </div>
                        <div class="col-sm-12">
                            <div class="col-sm-4"></div>
                            <div class="col-sm-8">
                                <div class="radio i-checks">
                                    <label class="no-padding">
                                        <input type="radio" value="1" name="hospital-doctor-radios"
                                               id="hospital-radio" style="position: absolute; opacity: 0;">
                                        <i></i>所有系统医生</label>
                                </div>
                                <div class="radio i-checks">
                                    <label class="no-padding">
                                        <input type="radio" value="2" name="hospital-doctor-radios"
                                               id="doctor-radio" style="position: absolute; opacity: 0;">
                                        <i></i>指定医生</label>

                                </div>
                                <!--<div class="radio">-->
                                <!--<label>-->
                                <!--<input type="radio" checked="checked" value="1" id="hospital-radio"-->
                                <!--name="hospital-doctor-radios">所有系统医生-->
                                <!--</label>-->
                                <!--</div>-->
                                <!--<div class="radio">-->
                                <!--<label>-->
                                <!--<input type="radio" value="2" id="doctor-radio" name="hospital-doctor-radios">-->
                                <!--指定医生-->
                                <!--</label>-->
                                <!--</div>-->
                            </div>
                            <div class="col-sm-4"></div>
                            <div class="col-sm-8 hide" id="hospital-district-div">
                                <div class="col-sm-3">
                                    <span class="form-control"
                                          style="padding-left: 0; padding-right: 0;border: none;line-height: 22px">选择医院：</span>
                                </div>
                                <div class="col-sm-2 no-padding">
                                    <span class="form-control"
                                          style="padding-left: 0; padding-right: 0;border: none;line-height: 22px">上海</span>
                                </div>
                                <div class="col-sm-5 no-padding">
                                    <select onchange="selectHospitalDistrict()" class="form-control m-b"
                                            id="hospital-district-select" name="hospital-district-select">
                                        <option value="0">请选择医院所在区</option>
                                        <option value="1">黄浦区</option>
                                        <option value="2">徐汇区</option>
                                        <option value="3">长宁区</option>
                                        <option value="4">静安区</option>
                                        <option value="5">普陀区</option>
                                        <option value="6">虹口区</option>
                                        <option value="7">杨浦区</option>
                                        <option value="8">宝山区</option>
                                        <option value="9">闵行区</option>
                                        <option value="10">嘉定区</option>
                                        <option value="11">浦东新区</option>
                                        <option value="12">松江区</option>
                                        <option value="13">金山区</option>
                                        <option value="14">青浦区</option>
                                        <option value="15">奉贤区</option>
                                        <option value="16">崇明县</option>
                                    </select>
                                </div>
                            </div>
                            <div class="col-sm-4"></div>
                            <div class="col-sm-8 hide" id="hospital-level-div">
                                <div class="col-sm-3">
                                    <!--选择医院：-->
                                </div>
                                <div class="col-sm-7 no-padding">
                                    <select onchange="selectHospitalLevel()" class="form-control m-b"
                                            id="hospital-level-select" name="hospital-level-select">
                                        <option value="0">请选择医院分类</option>
                                    </select>
                                </div>
                            </div>
                            <div class="col-sm-4"></div>
                            <div class="col-sm-8 hide" id="hospital-div">
                                <div class="col-sm-3">
                                    <!--选择医院：-->
                                </div>
                                <div class="col-sm-7 no-padding">
                                    <select onchange="selectHospital()" class="form-control m-b"
                                            id="hospital-select" name="hospital-select">
                                        <option value="0">请选择医院</option>
                                    </select>
                                </div>
                            </div>
                            <div class="col-sm-4"></div>
                            <div class="col-sm-8 hide" id="doctor-div">
                                <div class="col-sm-3">
                                    选择医生：
                                </div>
                                <div class="col-sm-7 no-padding">
                                    <select onchange="selectDoctor()" class="form-control m-b" id="doctor-select"
                                            name="doctor-select">
                                        <option value="0">请选择医生</option>
                                    </select>
                                </div>
                            </div>
                        </div>
                    </div>
                </form>
                <div class="row" style="margin-top: 20px">
                    <div class="col-sm-6 element-center">
                        <button data-dismiss="modal" class="btn btn-w-m btn btn-white inline-block element-center"
                                style="width: 102px;" onclick="">
                            取消
                        </button>
                    </div>
                    <div class="col-sm-6 element-center">
                        <button onclick="submit(this)" class="btn btn-w-m btn-primary inline-block element-center"
                                style="width: 102px;">
                            确定
                        </button>
                    </div>
                </div>
            </div>


        </div>
    </div>
    <!--</div>-->
</div>
</div>
<script src="/js/plugins/iCheck/icheck.min.js"></script>
<script>
	
    var currentPage1 = 1;
    var totalPage1 = 1;

    var currentPage2 = 1;
    var totalPage2 = 1;

    var currentPage3 = 1;
    var totalPage3 = 1;

    var pageSize = 10;

    var myPatientDivHtml = $("#my-patient-div");
    var outPatientDivHtml = $("#out-patient-div");
    var inPatientDivHtml = $("#in-patient-div");
    var hospitalDistrictSelect = $("#hospital-district-select");
    var hospitalLevelSelect = $("#hospital-level-select");
    var hospitalSelect = $("#hospital-select");
    var doctorSelect = $("#doctor-select");
    var myPatientHtml = $("#my-patient");

    var pagination1 = $('#pagination-1');
    var noRecords1 = $("#no-records-1");
    var pagination2 = $('#pagination-2');
    var noRecords2 = $("#no-records-2");
    var pagination3 = $('#pagination-3');
    var noRecords3 = $("#no-records-3");
    var searchKey = $("#search-key");
    var hospitalList = [];

    var myCount = 0;

    var hospitalId = getCookie("hospital");

    var formObj = {};
    var tableIdx = 0;
    var hospitalLevelList = ["一级医院", "二级医院", "三级医院"];
    var hospitalMap = {};

    $(document).ready(function () {

        getRelatedEMR(currentPage1, pageSize);

        buildDistrictSelect(109);

        getHospitalList();

        pageInit();

        show(2);

    });

    var offsetTop = 0;
    var modalDiv = $("#modal-transfer-div");
    modalDiv.on('touchmove', function (event) {
        event.preventDefault();
    }, false);
    $(window).scroll(function () {
        offsetTop = document.body.scrollTop ? document.body.scrollTop : document.documentElement.scrollTop;
        console.log(offsetTop);
        modalDiv.attr('style', 'position:absolute;overflow:auto;top:' + offsetTop + "px;");
    });
    modalDiv.on("shown.bs.modal", function () {
        $(".modal-backdrop").attr("style", "position:absolute;top:" + offsetTop + "px;bottom:-" + offsetTop + "px");
    });

    modalDiv.on("hidden.bs.modal", function () {
        myPageReset();
    });
    //配置model对话框
    //    $('#modal-transfer-div').on("show.bs.modal",function(){
    //        // 是弹出框居中。。。
    ////        var $modal_dialog = $(this).find('.modal-dialog');
    //        //获取可视窗口的高度
    ////        var clientHeight = (document.body.clientHeight < document.documentElement.clientHeight) ? document.body.clientHeight: document.documentElement.clientHeight;
    //        var clientHeight = window.screen.availHeight;
    //        //得到dialog的高度
    //        var dialogHeight = $(this).height();
    //        //计算出距离顶部的高度
    //        var m_top = (clientHeight - dialogHeight)/2;
    //        console.log("clientHeight : " + clientHeight);
    //        console.log("dialogHeight : " + dialogHeight);
    //        console.log("m_top : " + m_top);
    //        $(this).css({'margin': m_top + 'px auto'});
    //    });

    function search() {
        if (tableIdx == 1) {
            currentPage1 = 1;
            getRelatedEMR(currentPage1, pageSize);
        } else if (tableIdx == 2) {
            currentPage1 = 1;
            getOutEMR(currentPage1, pageSize);
        } else if (tableIdx == 3) {
            currentPage1 = 1;
            getInEMR(currentPage1, pageSize);
        }

    }

    function show(idx) {
        tableIdx = idx;

        var myPatientList = $("#my-patient-list");
        var outPatientList = $("#out-patient-list");
        var inPatientList = $("#in-patient-list");
        var myPatientButton = $("#my-patient-button");

        if (1 == idx) {
            getRelatedEMR(currentPage1, pageSize);
            outPatientList.addClass("hide");

            inPatientList.addClass("hide");

            myPatientList.removeClass("hide");
            myPatientButton.removeClass("hide");
        }
        else if (2 == idx) {
            getOutEMR(currentPage2, pageSize);

            myPatientList.addClass("hide");
            myPatientButton.addClass("hide");

            inPatientList.addClass("hide");

            outPatientList.removeClass("hide");
        }
        else if (3 == idx) {
            getInEMR(currentPage3, pageSize);
            myPatientList.addClass("hide");
            myPatientButton.addClass("hide");

            outPatientList.addClass("hide");

            inPatientList.removeClass("hide");
        }
    }
    
    //初始化页面
    function pageInit() {
        pagination1.jqPagination({
            max_page: totalPage1, //设置最大页 默认为1
            current_page: currentPage1, //设置当前页 默认为1
            page_string: '当前第{current_page}页,共{max_page}页',
            paged: function (page) {
                if (page != currentPage1) {
                    getRelatedEMR(page, pageSize);
                    currentPage1 = page;
                }
            }
        });

        pagination2.jqPagination({
            max_page: totalPage2, //设置最大页 默认为1
            current_page: currentPage2, //设置当前页 默认为1
            page_string: '当前第{current_page}页,共{max_page}页',
            paged: function (page) {
                if (page != currentPage2) {
                    getOutEMR(page, pageSize);
                    currentPage2 = page;
                }
            }
        });

        pagination3.jqPagination({
            max_page: totalPage3, //设置最大页 默认为1
            current_page: currentPage3, //设置当前页 默认为1
            page_string: '当前第{current_page}页,共{max_page}页',
            paged: function (page) {
                if (page != currentPage3) {
                    getInEMR(page, pageSize);
                    currentPage3 = page;
                }
            }
        });

        // Add responsive to jqGrid
        $("#my-patient-list").bind('resize', function () {
            var width1 = $('#my-patient-list').width();
            $("#my-patient").setGridWidth(width1);
        });

        $('#out-patient-list').bind('resize', function () {
            var width2 = $('#out-patient-list').width();
            $("#out-patient").setGridWidth(width2);
        });

        $('#in-patient-list').bind('resize', function () {
            var width3 = $('#in-patient-list').width();
            $("#in-patient").setGridWidth(width3);

        });
        
        mandy_get_profile(function (data) {
            if (data.institution.role.role == "主任") {
                $("#my-referral-count-button").removeClass("hide");
            }
        });
    }


    function getRelatedEMR(currentPage, pageSize) {
        var data = {};
        if (searchKey.val().length > 0) {
            mandy_get_all_emr(searchKey.val(), getCookie("hospital"), currentPage, pageSize, buildTable);
        } else {
        	mandy_get_my_emr(searchKey.val(), currentPage, pageSize, buildTable);
        }
//        mandy_get_related_emr(searchKey.val(),currentPage, pageSize, function (data) {


//        });
    }

    function buildTable(data) {
        currentPage1 = data.current_page;
        totalPage1 = data.total_page;
        if (totalPage1 == 0) {
            totalPage1 = 1;
        }
        pagination1.jqPagination('option', 'current_page', currentPage1);
        pagination1.jqPagination('option', 'max_page', totalPage1);

        if (data.list == null) {
            data.list = [];
            noRecords1.show();
        }
        else if (data.list.length != 0) {
            noRecords1.hide();
        }
        else {
            noRecords1.show();
        }

        myPatientDivHtml.empty();
        myPatientDivHtml.append("<table id='my-patient' style='text-align: center;'></table>");
//                alert("test");
//                debugger;
        var index = 1;
        $("#my-patient").jqGrid({
            data: data.list,
            datatype: "local",
            height: "auto",
            autowidth: true,
            shrinkToFit: true,
            rowNum: pageSize,
//                rownumbers: true,
            colNames: ['序号', /* '分类',  */'患者编号', '姓名', '性别', '年龄', '医保类型', /*'诊断',*/ '创建时间', "操作", "查看"],
            colModel: [
                {
                    name: 'index', index: 'index', width: 30, align: 'center',
                    formatter: function (cellvalue, options, rowObject) {
                        return index++;
                    }

                },
                /* {
                    name: 'clinic_list', index: '', width: 50, align: 'center',
                    formatter: function (cellvalue, options, rowObject) {
                        var maxTime = 0;
                        var di = "";
                        $.each(cellvalue, function (idx, val) {
                            if (val.create_time > maxTime) {
                                maxTime = val.create_time;
                                di = val.disease_info;
                            }
                        });
                        if (di != "") {
                            var d = JSON.parse(di);
                            var res = "";
                            $.each(d, function (idx, v) {
                                if (v.name == "patient_type") {
                                    res = v.text;
                                }
                            });
                            if (res != "") {
                                if (res == "1") {
                                    return "<span>门诊</span>";
                                } else if (res == "2") {
                                    return "<span>住院</span>";
                                }
                            }
                        }
                        return "<span>-</span>";
                    }

                }, */
                {
                    name: 'number', index: '', width: 130, align: 'center',
                    formatter: function (cellvalue, options, rowObject) {
                        return "<span data_my_data=" + rowObject.id + ">" + cellvalue + "</span>";
                    }

                },
                {
                    name: 'patient.name', index: 'patient.name', width: 50, align: 'center',
                    formatter: function (cellvalue, options, rowObject) {
                        return "<span data_my_data=" + rowObject.patient.id + ">" + getPrivatePatientName(cellvalue) + "</span>";
                    }
                },
                {
                    name: 'patient.sex', index: 'patient.sex', width: 50, align: 'center',
                    formatter: function (cellvalue, options, rowObject) {
                        if (cellvalue == "female") {
                            return "<span data_my_data=" + cellvalue + ">" + "女" + "</span>";
                        }
                        else if (cellvalue == "male") {
                            return "<span data_my_data=" + cellvalue + ">" + "男" + "</span>";
                        }
                        else {
                            return "<span>未知</span>";
                        }
                    }
                },
                {
                    name: 'patient.birthday', index: 'patient.birthday', width: 50, align: 'center',
                    formatter: function (cellvalue, options, rowObject) {
                        if (cellvalue != null) {
                            return "<span data_my_data=" + cellvalue + ">" + calculateAge(cellvalue) + "</span>";
                        }
                    }
                },
                {
                    name: 'insurance_type', index: 'insurance_type', width: 60, align: 'center',
                    formatter: function (cellvalue, options, rowObject) {
                        if (1 == cellvalue) {
                            return "<span data_my_data=" + cellvalue + ">" + "医保" + "</span>";
                        } else if (2 == cellvalue) {
                            return "<span data_my_data=" + cellvalue + ">" + "工伤" + "</span>";
                        }
                        else {
                            return "<span data_my_data=" + cellvalue + ">" + "自费" + "</span>";
                        }
                    }
                },
//                {
//                    name: 'clinic_list', index: 'clinic_list', width: 90, align: 'center',
//                    formatter: function (cellvalue, options, rowObject) {
//                        if (cellvalue != null && cellvalue.length > 0) {
//                            debugAlert(cellvalue[0]);
//                            return "<span class='' data_my_data=" + cellvalue[0].id + ">" + cellvalue[0].diagnose.name + "</span>";
//                        }
//                        else {
//                            return "<span></span>";
//                        }
//                    }
//                },
                {
                    name: 'create_time', index: 'create_time', width: 120, align: 'center',
                    formatter: function (cellvalue, options, rowObject) {
                        if (cellvalue != null) {
                            var dateTime = new Date(cellvalue).format("YYYY-MM-DD HH:mm");
                            return "<span data_my_data=" + cellvalue + ">" + dateTime + "</span>";
                        }
                        else {
                            return "<span></span>";
                        }
                    }
                },
                {
                    name: 'transfer-button',
                    index: 'transfer-button',
                    width: 50, align: 'center',
                    formatter: function (cellvalue, options, rowObject) {

                        var referralOption = '<a class="referral_' + rowObject.id + '" data-toggle="modal" href="#modal-transfer-div" style="text-decoration:none;color:#fff;padding:5px 10px;background-color: #36CDEA; border-radius: 14px;">转诊</a>';
                        $("#my-patient").on("click", ".referral_" + rowObject.id, function () {
                            buildTransferForm(rowObject);
                        });
                        return referralOption;
                    }
                },
                {
                    name: 'lookup',
                    index: 'lookup',
                    width: 50, align: 'center',
                    formatter: function (cellvalue, options, rowObject) {
                        debugAlert("cellValue");
                        debugAlert(rowObject);
                        var param = {};
                        param.emr_id = rowObject.id;

                        var a = $("<a>");
                        a.attr("style", "text-decoration:none;color:#fff;padding:5px 10px;background-color: #42D0A7; border-radius: 14px;");
                        a.attr("onclick", "setParamToUrl(" + JSON.stringify(param) + ");loadTitle('/emr/info-title.html');");
                        a.text("病历");
                        debugAlert(a.prop("outerHTML"));
                        return a.prop("outerHTML");
                    }
                }
            ],
            viewrecords: true,
            hidegrid: false,
            onSelectRow: function (trId, status) {
            },
            onSortCol: function (idx, iCol, sortorder) {
                index = index - data.list.length;
            }
        });

    	$("#my-patient tr:even").css("background","#F6F6F6")
    }

    function getOutEMR(currentPage, pageSize) {
        mandy_get_out_emr(searchKey.val(), currentPage, pageSize, function (data) {
            currentPage2 = data.current_page;
            totalPage2 = data.total_page;
            if (totalPage2 == 0) {
                totalPage2 = 1;
            }
            pagination2.jqPagination('option', 'current_page', currentPage2);
            pagination2.jqPagination('option', 'max_page', totalPage2);

            if (data.list == null) {
                data.list = [];
                noRecords2.show();
            }
            else if (data.list.length != 0) {
                noRecords2.hide();
            }
            else {
                noRecords2.show();
            }

            outPatientDivHtml.empty();
            outPatientDivHtml.append("<table id='out-patient' class=''></table>");
//                alert("test");
//                debugger;
            var index = 1;
            $("#out-patient").jqGrid({
                data: data.list,
                datatype: "local",
                height: "auto",
                autowidth: true,
                shrinkToFit: true,
                rowNum: pageSize,
//                rownumbers: true,
                colNames: ['序号', /* '分类',  */'患者编号', '姓名', '性别', '年龄', '医保类型', /*'诊断',*/ '转入医院', '转诊状态', '转诊时间', "查看"],
                colModel: [
                    {
                        name: 'index', index: 'index', width: 30, align: 'center',
                        formatter: function (cellvalue, options, rowObject) {
                            return index++;
                        }
                    },
                   /*  {
                        name: 'out_emr.clinic_list', index: '', width: 50, align: 'center',
                        formatter: function (cellvalue, options, rowObject) {
                            var maxTime = 0;
                            var di = "";
                            $.each(cellvalue, function (idx, val) {
                                if (val.create_time > maxTime) {
                                    maxTime = val.create_time;
                                    di = val.disease_info;
                                }
                            });
                            if (di != "") {
                                var d = JSON.parse(di);
                                var res = "";
                                $.each(d, function (idx, v) {
                                    if (v.name == "patient_type") {
                                        res = v.text;
                                    }
                                });
                                if (res != "") {
                                    if (res == "1") {
                                        return "<span>门诊</span>";
                                    } else if (res == "2") {
                                        return "<span>住院</span>";
                                    }
                                }
                            }
                            return "<span>-</span>";
                        }

                    }, */
                    {name: 'out_emr.number', index: 'out_emr.number', width: 130, align: 'center'},
                    {
                        name: 'out_emr.patient.name', index: 'out_emr.patient.name', width: 50, align: 'center',
                        formatter: function (cellvalue, options, rowObject) {
                            return getPrivatePatientName(cellvalue);
                        }
                    },
                    {
                        name: 'out_emr.patient.sex', index: 'out_emr.patient.sex', width: 50, align: 'center',
                        formatter: function (cellvalue, options, rowObject) {
                            if (cellvalue == "female") {
                                return "女";
                            }
                            else if (cellvalue == "male") {
                                return "男";
                            }
                            else {
                                return "未知";
                            }
                        }
                    },
                    {
                        name: 'out_emr.patient.birthday', index: 'out_emr.patient.birthday', width: 50, align: 'center',
                        formatter: function (cellvalue, options, rowObject) {
                            if (cellvalue != null) {
                                return calculateAge(cellvalue);
                            }
                        }
                    },
                    {
                        name: 'out_emr.insurance_type', index: 'out_emr.insurance_type', width: 80, align: 'center',
                        formatter: function (cellvalue, options, rowObject) {
                            if (1 == cellvalue) {
                                return "医保";
                            }
                            else if (2 == cellvalue) {
                                return "工伤";
                            }
                            else {
                                return "自费";
                            }
                        }
                    },
//                    {
//                        name: 'out_emr.clinic_list', index: 'out_emr.clinic_list', width: 90, align: 'center',
//                        formatter: function (cellvalue, options, rowObject) {
//                            if (cellvalue != null && cellvalue.length > 0) {
//                                debugAlert(cellvalue[0]);
//                                return "<span class='' data_my_data=" + cellvalue[0].id + ">" + cellvalue[0].diagnose.name + "</span>";
//                            }
//                            else {
//                                return "<span></span>";
//                            }
//                        }
//                    },
                    {
                        name: 'in_hospital.name', index: 'in_hospital.name', width: 120, align: 'center',
                        formatter: function (cellvalue, options, rowObject) {

                            if (rowObject.status == 0) {
                                var select = $("<select onchange='assignEMRMedical(this);' class='form-control' style='height: 30px;width: 100%;color: #676A6C;font-size: 13px;padding: 0;'>");
                                var indexTmp = index;
                                select.prop("id", "in_hospital_" + indexTmp);
                                var option = $("<option>");
                                option.attr("value", 0);
                                option.text("请选择医院");
                                select.append(option);

                                mandy_get_receive_doctor_list_sync(rowObject.id, function (data) {
                                    var list = data.list;
                                    if (list != null && list.length > 0) {
                                        for (var i = 0; i < list.length; i++) {
                                            var hospital = list[i].hospital;
                                            var medical = list[i].medical;
                                            var referral = list[i].referral_EMR;
                                            if (hospital != null && medical != null && referral != null) {
                                                var option = $("<option>");
                                                option.attr("value", medical.id);
                                                option.attr("referralId", referral.id);
                                                option.append(medical.name + "-" + hospital.name);
                                                select.append(option);
                                            }
                                        }
                                    }

                                });

                                return select.prop("outerHTML");
                            }
                            else {
                                return cellvalue;
                            }


                        }
                    },
                    {
                        name: 'status', index: 'status', width: 80, align: 'center',
                        formatter: function (cellvalue, options, rowObject) {
                            if (0 == cellvalue) {
                                return "进行中";
                            }
                            else if (3 == cellvalue) {
                                return "未完成";
                            }
                            else {
                                return "完成";
                            }
                        }
                    },
                    {
                        name: 'out_time', index: 'out_time', width: 120, align: 'center',
                        formatter: function (cellvalue, options, rowObject) {
                            if (cellvalue != null) {
                                return new Date(cellvalue).format("YYYY-MM-DD HH:mm");
                            }
                            else {
                                return "";
                            }
                        }
                    },
                    {
                        name: 'lookup', index: 'lookup', width: 60, align: 'center',
                        formatter: function (cellvalue, options, rowObject) {
                            var param = {};
                            param.emr_id = rowObject.out_emr.id;
                            param.transfer_id = rowObject.id;
                            param.type = 0;

                            var a = $("<a>");
                            a.attr("style", "text-decoration:none;color:#fff;padding:5px 10px;background-color: #42D0A7; border-radius: 14px;");
                            a.attr("onclick", "setParamToUrl(" + JSON.stringify(param) + ");loadTitle('/emr/info-title.html');");
                            a.text("病历");
                            debugAlert(a.prop("outerHTML"));
                            return a.prop("outerHTML");
                        }
                    }
                ],
                viewrecords: true,
                hidegrid: false,
                onSelectRow: function (rowid, status) {
                    var rowData = $('#gridTable').jqGrid('getRowData', rowid);
                    debugAlert(rowData);
                },
                onSortCol: function (idx, iCol, sortorder) {
                    index = index - data.list.length;
                }

            });
            $("#out-patient tr:even").css("background","#F6F6F6")
        });
        
    }

    function getInEMR(currentPage, pageSize) {
        mandy_get_in_emr(searchKey.val(), currentPage, pageSize, function (data) {
            currentPage3 = data.current_page;
            totalPage3 = data.total_page;
            if (totalPage3 == 0) {
                totalPage3 = 1;
            }
            pagination3.jqPagination('option', 'current_page', currentPage3);
            pagination3.jqPagination('option', 'max_page', totalPage3);

            if (data.list == null) {
                data.list = [];
                noRecords3.show();
            }
            else if (data.list.length != 0) {
                noRecords3.hide();
            }
            else {
                noRecords3.show();
            }

            inPatientDivHtml.empty();
            inPatientDivHtml.append("<table id='in-patient' class=''></table>");
//                alert("test");
//                debugger;
            var index = 1;
            $("#in-patient").jqGrid({
                data: data.list,
                datatype: "local",
                height: "auto",
                autowidth: true,
                shrinkToFit: true,
                rowNum: pageSize,
//                rownumbers: true,
                colNames: ['序号', /* '分类', */ '患者编号', '姓名', '性别', '年龄', '医保类型', /*'诊断',*/ '转出医院', '状态', '接诊时间', "接诊完成时间", "门诊", "查看"],
                colModel: [
                    {
                        name: 'index', index: 'index', width: 30, align: 'center',
                        formatter: function (cellvalue, options, rowObject) {
                            return index++;
                        }
                    },
                    /* {
                        name: 'out_emr.clinic_list', index: '', width: 50, align: 'center',
                        formatter: function (cellvalue, options, rowObject) {
                            var maxTime = 0;
                            var di = "";
                            $.each(cellvalue, function (idx, val) {
                                if (val.create_time > maxTime) {
                                    maxTime = val.create_time;
                                    di = val.disease_info;
                                }
                            });
                            if (di != "") {
                                var d = JSON.parse(di);
                                var res = "";
                                $.each(d, function (idx, v) {
                                    if (v.name == "patient_type") {
                                        res = v.text;
                                    }
                                });
                                if (res != "") {
                                    if (res == "1") {
                                        return "<span>门诊</span>";
                                    } else if (res == "2") {
                                        return "<span>住院</span>";
                                    }
                                }
                            }
                            return "<span>-</span>";
                        }

                    }, */
                    {
                        name: 'out_emr.number', index: 'out_emr.number', width: 120, align: 'center',
                        formatter: function (cellvalue, options, rowObject) {
                            return "<span data_my_data=" + rowObject.out_emr.id + " data_my_data2=" + rowObject.id + ">" + cellvalue + "</span>";
                        }
                    },
                    {
                        name: 'out_emr.patient.name', index: 'out_emr.patient.name', width: 50, align: 'center',
                        formatter: function (cellvalue, options, rowObject) {
                            return "<span data_my_data=" + rowObject.out_emr.patient.id + ">" + getPrivatePatientName(cellvalue) + "</span>";
                        }
                    },
                    {
                        name: 'out_emr.patient.sex', index: 'out_emr.patient.sex', width: 50, align: 'center',
                        formatter: function (cellvalue, options, rowObject) {
                            if (cellvalue == "female") {
                                return "<span data_my_data=" + cellvalue + ">" + "女" + "</span>";
                            }
                            else if (cellvalue == "male") {
                                return "<span data_my_data=" + cellvalue + ">" + "男" + "</span>";
                            }
                            else {
                                return "<span>未知</span>";
                            }
                        }
                    },
                    {
                        name: 'out_emr.patient.birthday', index: 'out_emr.patient.birthday', width: 50, align: 'center',
                        formatter: function (cellvalue, options, rowObject) {
                            if (cellvalue != null) {
                                return "<span data_my_data=" + cellvalue + ">" + calculateAge(cellvalue) + "</span>";
                            }
                            else {
                                return "<span></span>";
                            }
                        }
                    },
                    {
                        name: 'out_emr.insurance_type', index: 'out_emr.insurance_type', width: 80, align: 'center',
                        formatter: function (cellvalue, options, rowObject) {
                            if (1 == cellvalue) {
                                return "<span data_my_data=" + cellvalue + ">" + "医保" + "</span>";
                            }
                            else if (2 == cellvalue) {
                                return "<span data_my_data=" + cellvalue + ">" + "工伤" + "</span>";
                            }
                            else {
                                return "<span data_my_data=" + cellvalue + ">" + "自费" + "</span>";
                            }
                        }
                    },
//                    {
//                        name: 'out_emr.clinic_list', index: 'out_emr.clinic_list', width: 90, align: 'center',
//                        formatter: function (cellvalue, options, rowObject) {
//                            if (cellvalue != null && cellvalue.length > 0) {
//                                debugAlert(cellvalue[0]);
//                                return "<span class='' data_my_data=" + cellvalue[0].id + ">" + cellvalue[0].diagnose.name + "</span>";
//                            }
//                            else {
//                                return "<span></span>";
//                            }
//                        }
//                    },
                    {
                        name: 'out_hospital.name', index: 'out_hospital.name', width: 120, align: 'center',

                        formatter: function (cellvalue, options, rowObject) {
                            if (cellvalue != null) {
                                return "<span class='' data_my_data=" + rowObject.out_hospital.id + ">" + cellvalue + "</span>";
                            }
                            else {
                                return "<span></span>";
                            }
                        }
                    },
                    {
                        name: 'status', index: 'status', width: 80, hidden: true, align: 'center',
                        formatter: function (cellvalue, options, rowObject) {
                            if (1 == cellvalue) {
                                return "<span class='' data_my_data=" + cellvalue + ">" + "进行中" + "</span>";
                            }
                            else if (2 == cellvalue) {
                                return "<span class='' data_my_data=" + cellvalue + ">" + "完成" + "</span>";
                            }
                            else {
                                return "<span class='' data_my_data=" + cellvalue + ">" + "未知" + "</span>";
                            }
                        }
                    },
                    {
                        name: 'in_time', index: 'in_time', width: 110, align: 'center',
                        formatter: function (cellvalue, options, rowObject) {
                            if (cellvalue != null) {
                                var dateTime = new Date(cellvalue).format("YYYY-MM-DD HH:mm");
                                return "<span data_my_data=" + cellvalue + ">" + dateTime + "</span>";
                            }
                            else {
                                return "<span></span>";
                            }
                        }
                    },
                    {
                        name: 'in_finish_time', index: 'in_finish_time', width: 110, hidden: true, align: 'center',
                        formatter: function (cellvalue, options, rowObject) {
                            if (cellvalue != null) {
                                var dateTime = new Date(cellvalue).format("YYYY-MM-DD HH:mm");
                                return "<span data_my_data=" + cellvalue + ">" + dateTime + "</span>";
                            }
                            else {
                                return "<span></span>";
                            }
                        }
                    },
                    {
                        name: 'new_clinic', index: 'new_clinic', width: 60, align: 'center',
                        formatter: function (cellvalue, options, rowObject) {
                            var param = {};
                            param.transfer_id = rowObject.id;

                            var a = $("<a>");

                            if (rowObject.status == 2) {
                                a.text("已建");

                                //a.prop("disabled", "disabled");无效
                                a.css("color", "#fff");
                                a.css({"text-decoration": "none","background-color":"#FF9999","padding":"5px 10px","border-radius":"14px"});
                                a.attr("onclick", "setParamToUrl(" + JSON.stringify({emr_id: rowObject.in_emr.id}) + ");loadTitle('/emr/info-title.html');");
                            }
                            else {
                                a.text("新建");
                                a.css("color", "#fff");
                                a.css({"text-decoration": "none","background-color":"#36CDEA","padding":"5px 10px","border-radius":"14px"});
                                a.attr("onclick", "setParamToUrl(" + JSON.stringify(param) + ");loadTitle('/patient/new_transfer_medical_record_title.html');");
                            }
                            return a.prop("outerHTML");
                        }
                    },
                    {
                        name: 'lookup', index: 'lookup', width: 60, align: 'center',
                        formatter: function (cellvalue, options, rowObject) {
                            var param = {};
                            param.emr_id = rowObject.out_emr.id;
                            param.transfer_id = rowObject.id;
                            param.type = 1;

                            var a = $("<a>");
                            a.attr("style", "text-decoration:none;color:#fff;padding:5px 10px;background-color: #42D0A7; border-radius: 14px;");
                            a.attr("onclick", "setParamToUrl(" + JSON.stringify(param) + ");loadTitle('/emr/info-title.html');");
                            a.text("病历");
                            debugAlert(a.prop("outerHTML"));
                            return a.prop("outerHTML");
                        }
                    }
                ],
                viewrecords: true,
                hidegrid: false,
                onSelectRow: function (trId, status) {

                },
                onSortCol: function (idx, iCol, sortorder) {
                    index = index - data.list.length;
                }
			
              
                
            });
            $("#in-patient tr:even").css("background","#f6f6f6")
        });
        
    }

    function buildDistrictSelect(cityId){
        mandy_get_district_list(cityId, function(data){
            hospitalDistrictSelect.empty();
            hospitalDistrictSelect.append($("<option value='0'>请选择医院所在区</option>"))
            if(data.district_list.length == 0){
                console.log("该城市没有任何区县，请检查cityId是否正确");
                return;
            }
            $.each(data.district_list, function(idx, district){
                var option = $("<option value='" + district.id + "'>" + district.name + "</option>");
                hospitalDistrictSelect.append(option);
            });
        });
    }

    function selectHospitalDistrict() {
        var id = hospitalDistrictSelect.val();

        hospitalLevelSelect.empty();
        var option = $("<option>");
        var option1 = $("<option>");
        var option2 = $("<option>");
        option.attr("value", 0);
        option.text("请选择医院分类");
        hospitalLevelSelect.append(option);

        hospitalSelect.empty();
        option1.attr("value", 0);
        option1.text("请选择医院");
        hospitalSelect.append(option1);

        doctorSelect.empty();
        option2.attr("value", 0);
        option2.text("请选择医生");
        doctorSelect.append(option2);
        if (id != null && 0 != id) {
            var area = $("#hospital-district-div option:selected").text();
            makeHospitalLevelSelect(hospitalLevelList, hospitalLevelSelect, area);
        }
    }

    function selectHospitalLevel() {
        //debugger;
        var area = $("#hospital-district-div option:selected").text();
        var id = hospitalLevelSelect.val();

        hospitalSelect.empty();
        var option = $("<option>");
        option.attr("value", 0);
        option.text("请选择医院");
        hospitalSelect.append(option);

        doctorSelect.empty();
        var option = $("<option>");
        option.attr("value", 0);
        option.text("请选择医生");
        doctorSelect.append(option);
        if (id != null && 0 != id) {
            makeHospitalSelect(hospitalMap[area][id], hospitalSelect);
//            makeDoctorSelect(null, doctorSelect);
        }

//        debugger;
    }

    function selectHospital() {
        //debugger;
        var id = hospitalSelect.val();

        doctorSelect.empty();
        var option = $("<option>");
        option.attr("value", 0);
        option.text("请选择医生");
        doctorSelect.append(option);
        if (0 != id && id != null) {
            formObj.hospitalId = id;
            grey_get_medical_staff_list(null, id, null, "doctor", null, null, null, null, null, function (data) {
                if (data.medical_staff != null) {
                    makeDoctorSelect(data.medical_staff, doctorSelect);
                }
            });
        }
//        formObj.hospitalId = id;
//        types, hospitalId, divisionId, station, provinceId, cityId, districtId, tagId, sortType, success, fail

    }

    function selectDoctor() {
        //debugger;
        var id = doctorSelect.val();
        formObj.doctorId = id;
    }

    function makeDoctorSelect(list, obj) {
        obj.empty();
        var option = $("<option>");
        option.attr("value", 0);
        option.text("请选择医生");
        obj.append(option);

        if (list != null) {
            $.each(list, function (index, val) {
                var option = $("<option>");
                option.attr("value", val.profile_info.id);
                option.append(val.profile_info.name);
                obj.append(option);
            });
        }
    }
    //在此处对某区域某等级下的医院进行筛选
    function makeHospitalSelect(list, obj) {
        obj.empty();
//        console.log("+++++++++++++", list);
        var option = $("<option>");
        option.attr("value", 0);
        option.text("请选择医院");
        obj.append(option);

        if (list != null) {
            $.each(list, function (index, val) {
                if (hospitalId != val.id) {
                    var option = $("<option>");
                    option.attr("value", val.id);
                    option.text(val.name);
                    obj.append(option);
                }
            });
        }
    }
    //生成医院分类
    function makeHospitalAreaSelect(area, obj) {
        var levelMap = hospitalMap[area];
        if (levelMap != null) {
            for (var i = 0; i < list.length; i++) {
                var option = $("<option>");

                option.attr("value", i + 1);
                option.append(hospitalLevelList[i]);
                obj.append(option);
            }
        }
        console.log("^^^^^^^^^^^^^^^" + list);
        //debugger;
//        if (list != null) {
//            for (var i = 0; i < list.length; i++) {
//                var option = $("<option>");
//                option.attr("value", i + 1);
//                option.append(list[i]);
//                obj.append(option);
//            }
//        }
    }
    function makeHospitalLevelSelect(list, obj, area) {
        obj.empty();
        var option = $("<option>");
        option.attr("value", 0);
        option.text("请选择医院分类");
        obj.append(option);

        //debugger;
        if (list != null) {
            for (var i = 0; i < list.length; i++) {
                var option = $("<option>");
                option.attr("value", i + 1);
                option.text(list[i]);
                if (hospitalMap[area][i + 1] == null) {
                    option.attr("disabled", true);
                }
                obj.append(option);
            }
        }
    }
    //获取所有医院的信息，并按照医院等级、区域进行存储在hospitalMap中
    function getHospitalList() {
        grey_get_hospital_list(null, null, function (data) {
            hospitalMap = {};
            if (data.hospital_list != null) {
                $.each(data.hospital_list, function (idx, val) {

                    var arr = hospitalMap[val.district.name];
                    arr = arr == null ? {} : arr;

                    var arr1 = arr[val.level + ""];
                    arr1 = arr1 == null ? [] : arr1;

                    arr1.push(val);
                    arr[val.level + ""] = arr1;

                    hospitalMap[val.district.name] = arr;
                });
            }
            var options = $("#hospital-district-div option");
            $.each(options, function (idx, option) {
                if (hospitalMap[$(option).text()] == null && $(option).text().indexOf("请选择") == -1) {
                    $(option).attr("disabled", true);
                }
            })
        });
    }

    function designatedPage(e, idx) {
        var ev = window.event || e;
        var pageNum = 1;
        var totalPage = 1;
        if (1 == idx) {
            pageNum = $("#page-num-1").val();
            totalPage = totalPage1;
        }
        else if (2 == idx) {
            pageNum = $("#page-num-2").val();
            totalPage = totalPage2;
        }
        else if (3 == idx) {
            pageNum = $("#page-num-3").val();
            totalPage = totalPage3;
        }

        if (ev.keyCode == 13) {
            if (!/^[0-9]*$/.test(pageNum) || pageNum > totalPage || pageNum < 1) {
                toastr.error("请输入正确的页数!");
                return;
            }
        }
    }

    //    $("input[type=radio][name='hospital-doctor-radios']").bind("change", function () {
    //        if (2 == $("input[type=radio][name='hospital-doctor-radios']:checked").val()) {
    //            $("#hospital-div").removeClass("hide");
    //            $("#doctor-div").removeClass("hide");
    //        }
    //        else {
    //            $("#hospital-div").addClass("hide");
    //            $("#doctor-div").addClass("hide");
    //        }
    //    });


    function buildTransferForm(rowObject) {

        $('.i-checks').iCheck({
            checkboxClass: 'icheckbox_square-green',
            radioClass: 'iradio_square-green'
        });

        $(".i-checks").on("ifChecked", function (event) {
            $(this).iCheck("check");
            if (2 == $("input[type=radio][name='hospital-doctor-radios']:checked").val()) {
                $("#hospital-district-div").removeClass("hide");
                $("#hospital-level-div").removeClass("hide");
                $("#hospital-div").removeClass("hide");
                $("#doctor-div").removeClass("hide");
            }
            else {
                $("#hospital-district-div").addClass("hide");
                $("#hospital-level-div").addClass("hide");
                $("#hospital-div").addClass("hide");
                $("#doctor-div").addClass("hide");
            }
            if (3 != $("input[type=radio][name='new-remark']:checked").val()) {
                $("input[type=text][name='remark-text']").attr("disabled", true);
            } else {
                $("input[type=text][name='remark-text']").attr("disabled", false);
            }
//            console.log($("input[type=radio][name='hospital-doctor-radios']:checked").val());
        });

//        $(".i-checks").on("ifUnChecked", function (event) {
//            $(this).iCheck("uncheck");
//            console.log($("input[type=radio][name='hospital-doctor-radios']:checked").val());
//        });

        if (rowObject == null) {
            return;
        }

        var myPatient = {};
        myPatient.EMRnum = rowObject.number;
        myPatient.EMRId = rowObject.id;

        myPatient.patientName = rowObject.patient.name;
        myPatient.patientId = rowObject.patient.id;

        myPatient.patientSex = sexArray[rowObject.patient.sex];
        myPatient.patientSexVal = rowObject.patient.sex;

        myPatient.patientAge = calculateAge(rowObject.patient.birthday);
        myPatient.patientAgeVal = rowObject.patient.birthday;

        myPatient.HIStype = HISTypeArray[rowObject.insurance_type];
        myPatient.HIStypeVal = rowObject.insurance_type;

        var clinics = rowObject.clinic_list;
        if (clinics != null && clinics.length > 0) {
//            myPatient.diagnose = clinics[0].diagnose.name;
            myPatient.clinicId = clinics[0].id;
        }

        myPatient.createDate = new Date(rowObject.create_time).format("YYYY-MM-DD HH:mm");
        myPatient.createDateVal = rowObject.create_time;

        formObj.EMRId = myPatient.EMRId;
//        formObj.clinicId = myPatient.clinicId;

        $("#my-patient-num").text(myPatient.EMRnum);
        $("#my-patient-name").text(getPrivatePatientName(myPatient.patientName));
        $("#my-patient-sex").text(myPatient.patientSex);
        $("#my-patient-age").text(myPatient.patientAge);
        $("#my-patient-type").text(myPatient.HIStype);
        $("#my-patient-diagnose").text(myPatient.diagnose);
        $("#lookup-patient-emr-btn").attr("onclick", "lookup(" + myPatient.EMRId + ")");
    }

    function submit(ele) {
        $(ele).attr("disabled", true);
        var patientType = $("select[name='patient-type'] option:selected").text();
        var newRemark = "";
        var remark = "转诊患者是" + patientType;
        var remarkChecked = $("input[type=radio][name='new-remark']:checked");
        if (typeof (remarkChecked.val()) == "undefined" || remarkChecked.val() == null) {
            toastr.error("请勾选转诊备注");
            $(ele).attr("disabled", false);
            return;
        }
        if (3 != remarkChecked.val()) {
            newRemark = $($(remarkChecked.parent()).parent()).text().trim();
        } else {
            newRemark = $("input[type=text][name='remark-text']").val();
            if (newRemark.length > 128) {
                toastr.error("描述信息请不要超过128个字！");
                $(ele).attr("disabled", false);
                return;
            }
            newRemark = newRemark.trim() == "" || newRemark == null ? "其他情况" : newRemark;
        }
        if (newRemark != null && newRemark.trim() != "") {
            remark += "," + newRemark;
        }
        var hospitalId = null;
        var doctorId = null;

        var optionDoctor = $("input[name=hospital-doctor-radios]:checked").val();
        if (typeof(optionDoctor) == 'undefined') {
            toastr.error('请选择转诊通知接收者');
            $(ele).attr("disabled", false);
            return;
        } else {
            if (optionDoctor == 2) {
                if ($("#hospital-district-select").val() == 0) {
                    toastr.error("请选择医院所在区");
                    $(ele).attr("disabled", false);
                    return;
                }

                if ($("#hospital-level-select").val() == 0) {
                    toastr.error("请选择医院分类");
                    $(ele).attr("disabled", false);
                    return;
                }

                if ($("#hospital-select").val() == 0) {
                    toastr.error("请选择医院");
                    $(ele).attr("disabled", false);
                    return;
                }

                if ($("#doctor-select").val() == 0) {
                    toastr.error("请选择医生");
                    $(ele).attr("disabled", false);
                    return;
                }

                hospitalId = formObj.hospitalId;
                doctorId = formObj.doctorId;
            }
        }

        mandy_post_out_emr(formObj.EMRId, hospitalId, doctorId, remark, function (data) {
            $(ele).attr("disabled", false);
//            alert("转出成功！");
            $("#modal-transfer-div").modal('hide');
            $("#out-patient-a").click();
        }, function (data){
            toastr.error(data.result_msg);
            $(ele).attr("disabled", false);
        });
    }

    function lookup(emrId) {
        setTimeout("delayJump(" + emrId + ")", 500);
    }

    function delayJump(emrId) {
        var param = {};
        param.emr_id = emrId;
        setParamToUrl(param);
        loadTitle('/emr/info-title.html');
    }

    function setJudgementCount(event, countId) {
        var num = 128 - $(event).val().length;

        var judgementCount = $("#" + countId);

        if (num > -1) {
            judgementCount.text("还可以输入" + num + "个字");
            judgementCount.removeClass("warning-color");
        } else {
            judgementCount.text("已超出" + (0 - num) + "个字");
            judgementCount.addClass("warning-color");
        }
    }

    function myPageReset() {
        $('#referral-note-form').get(0).reset();
        $('#judgement_count').text('还可以输入128个字');
//        makeHospitalLevelSelect(null, hospitalLevelSelect);
        makeHospitalLevelSelect(null, hospitalLevelSelect, null);
        makeHospitalSelect(null, hospitalSelect);
        makeDoctorSelect(null, doctorSelect);
        $("#hospital-district-div").addClass("hide");
        $('#hospital-level-div').addClass('hide');
        $('#hospital-div').addClass('hide');
        $('#doctor-div').addClass('hide');
        $("#judgement_count").removeClass("warning-color");
    }

    function assignEMRMedical(element) {
        debugger;
        var ele = $(element);
        var inMedicalId = ele.val();
        var referralEMRId = ele.find("option:selected").attr("referralId");
        if (0 == inMedicalId) {
            toastr.error("请选择医生");
            return;
        }

        var r = confirm("确定指定这个医生?")
        if (r == true) {
            mandy_put_receive_medical(referralEMRId, inMedicalId, function (data) {
                toastr.success("指定医生成功!");
                getOutEMR(currentPage2, pageSize);
            });
        }

    }
    
    
   
</script>


</body>

</html>
